package sort;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Objects;

//因为这个类的实例对象要作为map输出的key，所以要实现writablecomparalbe接口
public class NumberStr implements WritableComparable<NumberStr> {
    //定义属性
    private Long index;//排序
    private Long value;//实际数值


    //准备构造方法

    public NumberStr() {
    }
    public NumberStr(Long index, Long value) {
        this.index = index;
        this.value = value;
    }



    public Long getIndex() {
        return index;
    }

    public void setIndex(Long index) {
        this.index = index;
    }

    public Long getValue() {
        return value;
    }

    public void setValue(Long value) {
        this.value = value;
    }

    //序列化方法
    @Override
    public void write(DataOutput out) throws IOException {
        out.writeLong(index);
        out.writeLong(value);

    }

    //反序列化方法
    @Override
    public void readFields(DataInput in) throws IOException {
        this.index = in.readLong();
        this.value = in.readLong();

    }

    //指定排序规则,我们希望按照总时长进行排序
    @Override
    public int compareTo(NumberStr o) {  //返回值三种：0：相等 1：小于 -1：大于
        System.out.println("compareTo 方法执行了。。。");
        //指定按照bean对象的总时长字段的值进行比较
        if (this.value > o.value) {
            return 1;
        } else if (this.value < o.value) {
            return -1;
        } else {
            return 0; //加入第二个判断条件，二次排序
        }

    }

    @Override
    public boolean equals(Object o) {
        System.out.println("equals方法执行了。。。");
       return super.equals(o);
    }

    @Override
    public int hashCode() {
        return Objects.hash(getValue());
    }

    @Override
    public String toString() {
        return index +
                "\t" + value
                ;
    }
}
